package org.fu.crowd.filter;

import org.fu.crowd.constant.AccessPassResource;
import org.fu.crowd.constant.CrowdConstant;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.RequestPath;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebSession;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

@Component
public class CrowdAccessFilter implements GlobalFilter, Ordered {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        RequestPath path = request.getPath();
        String string = path.toString();
        boolean flagresult = AccessPassResource.PASS_RES_SET.contains(string);
        if (flagresult){
            return chain.filter(exchange);
        }
        boolean result = AccessPassResource.judgeCurrentServletPathWeatherStaticResource(string);
        if (result){
            return chain.filter(exchange);
        }

        HttpCookie first = request.getCookies().getFirst("SESSION");
        System.out.println(first);
        if (first==null){
            System.out.println("true");
            String url = "http://www.crowd.com/member/login";
            ServerHttpResponse response = exchange.getResponse();
            response.getHeaders().set(HttpHeaders.LOCATION,url);
            response.setStatusCode(HttpStatus.SEE_OTHER);
            response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
            return response.setComplete();
        }else {
            return chain.filter(exchange);
        }
//        exchange.getSession().subscribe(webSession -> {
//            Object attribute = webSession.getAttribute(CrowdConstant.LOGIN_MEMBER);
//            if (attribute!=null){
//                atomicBoolean.set(true);
//            }
//        });
//        if (!atomicBoolean.get()){
//            System.out.println("true");
//            String url = "http://www.crowd.com/member/login";
//            ServerHttpResponse response = exchange.getResponse();
//            response.getHeaders().set(HttpHeaders.LOCATION,url);
//            response.setStatusCode(HttpStatus.SEE_OTHER);
//            response.getHeaders().add("Content-Type", "text/plain;charset=UTF-8");
////            response.getHeaders().set("Referer","http://www.crowd.com/member/login");
//            return response.setComplete();
//        }else {
//            return chain.filter(exchange);
//        }
    }

    @Override
    public int getOrder() {
        return 0;
    }
}
